<?php

namespace IMT\Plugins\AttachmentsBundle\Controller;

use IMT\EventBundle\Entity\Event;

use IMT\AttachmentsBundle\Entity\Attachment;

use Symfony\Component\Routing\Exception\MethodNotAllowedException;
use Symfony\Component\Security\Core\Exception\AccessDeniedException;
use Symfony\Component\HttpKernel\Exception\NotFoundHttpException;
use Symfony\Component\HttpFoundation\Response;
use Symfony\Bundle\FrameworkBundle\Controller\Controller;


class DefaultController extends Controller
{
    
    public function indexAction()
    {
        return $this->forward('IMTPluginsAttachmentsBundle:Default:show', array(
        	'idEvent' => 0
    	));
    }
    
    public function showAction($idEvent, $type)
    {
    	$emEvent = $this->get('imt_event.'.$type.'_manager');
    	$securityContext = $this->get('security.context');
    	$user = $securityContext->getToken()->getUser();
    	 
    	$participants = array();
    	
    	if($idEvent != 0){
    		$event = $emEvent->findEventBy(array('id' => $idEvent));
    		 
    		if(!$event)
    			throw $this->createNotFoundException('L\'événement recherché n\'existe pas');
    	} else 
    		$event = $emEvent->createEvent();
    	    	 
    	return $this->render('IMTPluginsAttachmentsBundle:Default:index.html.twig', array(
    		'event' => $event,
    	));
    }
    
    public function updateAJAXAction($idEvent, $type)
    {
    	$return = array('result' => 500, 'msg' => 'Une erreur inconnue est survenue');
    
    	try {
    		$request = $this->getRequest();
    		$em = $this->getDoctrine()->getEntityManager();
    		 
    		if('POST' == $request->getMethod()){
    
    			$emEvent = $this->container->get('imt_event.'.$type.'_manager');
    			$validator = $this->get('validator');
    			 
    			if($idEvent != 0){
    				$event = $emEvent->findEventBy(array('id' => $idEvent));
    			}
    			 
    			if(!$event)
    				throw $this->createNotFoundException('L\'événement à modifier n\'existe pas');
    			
    			$idAttachments = $request->request->get('id_attachments');
    			
    			$repoAttachment = $this->getDoctrine()->getRepository('IMTAttachmentsBundle:Attachment');
    			    			
    			$return['result'] = 200;
    			$return['msg'] = 'Aucune pièce jointe n\'a été ajoutée';
    			
    			if(is_array($idAttachments)){
    				foreach($idAttachments as $idAttachment){  
    					$attachment = $repoAttachment->find($idAttachment);
    					 
    					if($attachment)
    						$event->addAttachment($attachment);
    				}
    				
    				$em->persist($event);
    				$em->flush();
    				
    				$return['result'] = 200;
    				$return['msg'] = 'Les pièces jointes ont été ajoutées';
    				   				
    				
    			}
    		} else {
    			throw new MethodNotAllowedException('POST');
    		}
    	} catch(NotFoundHttpException $e){
    		return new Response(json_encode(array('result' => 500, 'msg' => $e->getMessage())));
    	} catch(AccessDeniedException $e){
    		return new Response(json_encode(array('result' => 500, 'msg' => $e->getMessage())));
    	} catch(\Exception $e){
    		return new Response(json_encode(array(
    				'result' => 500,
    				'msg' => 'Une erreur est survenue au niveau du serveur',
    				'debug' => $e->getMessage()
    		)));
    	}
    	 
    	return new Response(json_encode($return));
    }
}
